//
//  CollectViewController.m
//  quanminzhekou
//
//  Created by zxl on 15/1/8.
//  Copyright (c) 2015年 zhongqinglongtu. All rights reserved.
//

#import "CollectViewController.h"
#import "CollectTableViewCell.h"
#import "CommonCellViewModel.h"
#import "DetailViewController.h"
#import "CollectManager.h"

@interface CollectViewController () <SwipeTableViewCellDelegate>
@property (nonatomic) NSMutableArray* readyDelArr;
@end

@implementation CollectViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    _readyDelArr = [[NSMutableArray alloc] init];
    
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    _dataArr = [CollectManager shareManager].collectArr;
    [self.tableView reloadData];
}

#pragma mark - IBAction
- (IBAction)onTapBackBtn:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}

- (IBAction)onTapDelBtn:(id)sender {
    self.delBtn.selected = !self.delBtn.selected;
    
    [[UIApplication sharedApplication] beginIgnoringInteractionEvents];
    __weak typeof(self) weakSelf = self;
    [UIView animateWithDuration:.5 animations:^{
        weakSelf.delToolBar.transform = weakSelf.delBtn.selected ? CGAffineTransformMakeTranslation(0, -44): CGAffineTransformIdentity;
    } completion:^(BOOL finished) {
        [[UIApplication sharedApplication] endIgnoringInteractionEvents];
    }];
    
    if (!self.delBtn.selected) {
        [self.readyDelArr removeAllObjects];
        [self.tableView reloadData];
    }
}

- (IBAction)onTapAllBtn:(id)sender {
    if (![self.dataArr isKindOfClass:[NSMutableArray class]] || 0 == self.dataArr.count) {
        return;
    }
    
    [self.readyDelArr removeAllObjects];
    self.readyDelArr = nil;
    self.readyDelArr = [NSMutableArray arrayWithArray:self.dataArr];
    
    [self.tableView reloadData];
}

- (IBAction)onTapDelSelected:(id)sender {
    if ([self.readyDelArr isKindOfClass:[NSMutableArray class]] && self.readyDelArr.count > 0) {
        __weak typeof(self) weakSelf = self;
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
            [weakSelf.dataArr removeObjectsInArray:self.readyDelArr];
            dispatch_async(dispatch_get_main_queue(), ^{
                [weakSelf.tableView reloadData];
            });
        });
    }
}

#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    CollectTableViewCell *cell = (CollectTableViewCell*)[tableView dequeueReusableCellWithIdentifier:@"CollectCell"];
    cell.delegate = self;
    cell.type = CollectTableViewCellTypeNone;
    
    CommonCellViewModel* viewModel = [self.dataArr objectAtIndex:indexPath.row];
    [cell layoutSubviewDataWithViewModel:viewModel];
    
    
    for (CommonCellViewModel* model in self.readyDelArr) {
        if ([viewModel.goodsID isEqualToString:model.goodsID]) {
            cell.type = CollectTableViewCellTypeIsReadyDelete;
        }
    }
    
    if (CollectTableViewCellTypeGoodsIsOut == cell.type || CollectTableViewCellTypeGoodsUpComing == cell.type) {
        cell.isHasSelectedType = NO;
    } else {
        cell.isHasSelectedType = YES;
    }
    
    return cell;
}

#pragma mark - UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (self.delBtn.selected) {
        CollectTableViewCell* cell = (CollectTableViewCell*)[tableView cellForRowAtIndexPath:indexPath];
        if (cell.type != CollectTableViewCellTypeIsReadyDelete) {
            [self.readyDelArr addObject:[self.dataArr objectAtIndex:indexPath.row]];
        } else {
            [self.readyDelArr removeObject:[self.dataArr objectAtIndex:indexPath.row]];
        }
        
        [tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else {
        CollectTableViewCell* cell = (CollectTableViewCell*)[tableView cellForRowAtIndexPath:indexPath];
        if (cell.isSwiping || CollectTableViewCellTypeGoodsUpComing == cell.type || CollectTableViewCellTypeGoodsIsOut == cell.type) {
            return;
        }
        [tableView deselectRowAtIndexPath:indexPath animated:YES];
        
        CommonCellViewModel* viewModel = [self.dataArr objectAtIndex:indexPath.row];
        
        DetailViewController* controller = [self.storyboard instantiateViewControllerWithIdentifier:@"DetailViewController"];
        controller.currentGoodsViewModel = viewModel;
        [self.navigationController pushViewController:controller animated:YES];
    }
}

#pragma mark - SwipeTableViewCellDelegate
-(void)swipeTableViewCellDidResetState:(SwipeTableViewCell*)swipeTableViewCell fromPoint:(CGPoint)point velocity:(CGPoint)velocity {
    CollectTableViewCell* cell = (CollectTableViewCell*)swipeTableViewCell;
    if (cell.isCollected || !cell.isDelWhenNotCollect) {
        return;
    }
    NSIndexPath* indexPath = [self.tableView indexPathForCell:swipeTableViewCell];
    [self.tableView beginUpdates];
    [self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
    [self.tableView endUpdates];
}

@end
